package com.learn.springlearn.controller;

import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.learn.springlearn.event.SpringEventTest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("event")
public class EventController {


    @Autowired
    ApplicationContext applicationContext;

    @GetMapping("send")
    @Cacheable(value = "smp")
    public String publish() throws InterruptedException {
        applicationContext.publishEvent(new SpringEventTest(this,"233333"));

//        LoadingCache<Object, Object> cache = CacheBuilder.newBuilder().maximumSize(1000).build(new CacheLoader<Object, Object>() {
//            @Override
//            public Object load(Object o) throws Exception {
//                TimeUnit.MINUTES.sleep(1);
//                return "smp";
//            }
//        });
//        Object unchecked = cache.getUnchecked(1);
//        System.out.println("缓存中获取到值："+unchecked);
        TimeUnit.SECONDS.sleep(5);
        return "hhh";
    }

    @CachePut(value = "smp")
    @GetMapping("update")
    public String updateCache(){
        return "update";
    }





}


